﻿using System;
using System.ComponentModel.DataAnnotations;
using Framework.Attributes;
using Framework.Model;

namespace CharacterModel
{
    public class CharacterInfo : IId
    {
        public int Id { get; set; }
        public Portrait Portrait { get; set; }
        [Required]
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Occupation { get; set; }
        public DateTime DateOfBirth { get; set; }
        public string Hobbies { get; set; }
        public string Education { get; set; }

        [Exclude]
        public string AgeOrDateOfBirth
        {
            get
            {
                if (DateOfBirth == default(DateTime))
                    return string.Empty;
                return DateOfBirth.ToString();
            }
            set
            {
                int result;
                if (int.TryParse(value, out result))
                {
                    DateOfBirth = DateTime.Now.Subtract(TimeSpan.FromDays(365*result));
                    DateOfBirth = new DateTime(DateOfBirth.Year, 1, 1);
                    return;
                }
                DateTime dtResult;
                if (!DateTime.TryParse(value, out dtResult))
                    throw new InvalidOperationException(string.Format("Could not parse an Age/Birth Date value of '{0}'", value));
                DateOfBirth = dtResult;
            }
        }
    }
}
